package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.AnonymousAccess;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.vo.Result;
import com.ruoyi.system.constant.StatisticConst;
import com.ruoyi.system.domain.StatisticWebsite;
import com.ruoyi.system.mapper.StatisticWebsiteMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/statisticWebsite")
@Api(tags="统计网站数据")
public class StatisticWebsiteController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private StatisticWebsiteMapper statisticWebsiteMapper;

    @GetMapping("/behavior")
    @ApiOperation(value="统计pv,uv数据", notes="统计pv,uv数据")
    @AnonymousAccess
    public Result<?> behavior(HttpServletRequest request) {
        String now = DateUtils.getDate8();
        stringRedisTemplate.opsForSet().add(String.format(StatisticConst.UV, now),
                IpUtils.getIpAddr(request));
        stringRedisTemplate.opsForValue().increment(String.format(StatisticConst.PV, now));
        return Result.OK();
    }

    @GetMapping("/behavior/actual")
    @ApiOperation(value="实时获取当天的PV、UV", notes="实时获取当天的PV、UV")
    public Result<?> actual() {
        String now = DateUtils.getDate8();
        Long uv = stringRedisTemplate.opsForSet().size(String.format(StatisticConst.UV, now));
        String pv = stringRedisTemplate.opsForValue().get(String.format(StatisticConst.PV, now));
        StatisticWebsite statisticWebsite = new StatisticWebsite();
        statisticWebsite.setUv(Objects.isNull(uv) ? 0 : uv);
        statisticWebsite.setPv(StringUtils.isBlank(pv) ? 0 : Long.parseLong(pv));
        statisticWebsite.setFlowDate(Integer.parseInt(now));
        return Result.OK(statisticWebsite);
    }

    @GetMapping("/behavior/get")
    @ApiOperation(value="获取pv,uv数据", notes="获取pv,uv数据")
    public Result<?> getBehavior(@RequestParam("day") Integer day) {
        Date now = DateUtils.getNowDate();
        List<Integer> dates = DateUtils.generateDateRange(DateUtils.addDays(now, -day), DateUtils.addDays(now, -1));
        List<StatisticWebsite> statisticWebsites = statisticWebsiteMapper.selectList(new LambdaQueryWrapper<StatisticWebsite>()
                .ge(StatisticWebsite::getFlowDate, dates.get(0))
                .le(StatisticWebsite::getFlowDate, dates.get(dates.size() - 1))
        );
        Map<Integer, StatisticWebsite> statisticWebsiteMap = statisticWebsites.stream()
                .collect(Collectors.toMap(StatisticWebsite::getFlowDate, StatisticWebsite -> StatisticWebsite));
        List<Long> pvData = new ArrayList<>(dates.size());
        List<Long> uvData = new ArrayList<>(dates.size());
        dates.forEach(date -> {
            StatisticWebsite temp = statisticWebsiteMap.get(date);
            if(Objects.nonNull(temp)){
                pvData.add(temp.getPv());
                uvData.add(temp.getUv());
            }else{
                pvData.add(null);
                uvData.add(null);
            }
        });

        Map<String, Object> result = new HashMap<>(3);
        result.put("dates", dates);
        result.put("pvData", pvData);
        result.put("uvData", uvData);
        return Result.OK(result);
    }

}
